package com.hr;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import com.espirit.common.VersionAware;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.manager.Department;
import com.espirit.eap.sa.Employee;
import com.espirit.eap.sa.EmployeeAware;
import com.espirit.eap.sa.User;
import com.espirit.finance.core.AcctTerm;
import com.espirit.finance.core.AcctTermCheckAware;
import com.espirit.finance.core.Voucher;

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "salaryItemType", discriminatorType = DiscriminatorType.STRING)
@Entity
@DomainModel
@Table(indexes={
		@Index(name="idx_typeaccttermemp", columnNames={"salaryItemType", "acctTerm_id", "employee_id"} ),
		},appliesTo = "hr_salaryitembase")
public class SalaryItemBase extends Voucher implements EmployeeAware, VersionAware, AcctTermCheckAware{
	private static final long serialVersionUID = -725217932117228763L;
	
	@ManyToOne(fetch=FetchType.LAZY)
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@ManyToOne
	public Salary getParent() {
		return parent;
	}
	public void setParent(Salary parent) {
		this.parent = parent;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public User getCreated() {
		return created;
	}
	public void setCreated(User created) {
		this.created = created;
	}
	
	@Transient
	public String getDeptIds() {
		return deptIds;
	}
	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}

	private Employee employee;
	private Salary parent;
	private Department department;
	private User created;
	private String deptIds;
	
	@Override
	public boolean check(AcctTerm acctTerm) {
		if (acctTerm != null && acctTerm.getBeginDate() != null && this.getAcctTerm() != null){
			return acctTerm.getName().compareTo(super.getAcctTerm().getName()) <= 0;
		} else {
			return true;
		}
	}
}
